package com.small.push.manager.impl;

import java.io.IOException;
import java.util.Objects;

import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.small.common.base.impl.BaseManagerImpl;
import com.small.push.config.MsgPushConfig;
import com.small.push.constant.PushConstants;
import com.small.push.dto.WeChatTemplateMsgDTO;
import com.small.push.dto.WeChatTemplateMsgResponse;
import com.small.push.manager.WeChatTemplateMsgManager;

/**
 * 微信模板消息
 * @author dingyou
 * @version 1.0*
 * @date 2019-01-10
 */
@Component
public class WeChatTemplateMsgManagerImpl extends BaseManagerImpl implements WeChatTemplateMsgManager {

    private final MsgPushConfig msgPushConfig;
//    private final AccessTokenManager accessTokenManager;
//    private final WxPushMsgRecordDao msgRecordDao;
//    private final UserWxDao userWxDao;

    public WeChatTemplateMsgManagerImpl(MsgPushConfig msgPushConfig) {
        this.msgPushConfig = msgPushConfig;
//        this.accessTokenManager = accessTokenManager;
//        this.msgRecordDao = msgRecordDao;
//        this.userWxDao = userWxDao;
    }

    /**
     * 发送微信公众号模板消息
     * @param templateMsgDTO
     */
    @Override
    public boolean sendTemplateMsg(WeChatTemplateMsgDTO templateMsgDTO, Long fromUser) {
        WeChatTemplateMsgResponse response = doSend(templateMsgDTO);
        // 为了下面的逻辑好统一处理,不样响应为null
        if(Objects.isNull(response)){
            response = new WeChatTemplateMsgResponse();
        }
        // 记录发送记录
//        WxPushMsgRecord record = new WxPushMsgRecord()
//                .setOpenid(templateMsgDTO.getToUser())
//                .setMessage(JSON.toJSONString(templateMsgDTO))
//                .setErrCode(response.getErrCode())
//                .setMsgId(response.getMsgId())
//                .setErrMsg(response.getErrMsg())
//                .setFromUser(fromUser);
//        msgRecordDao.insert(record);
        return Objects.equals(response.getErrCode(), PushConstants.WECHAT_MSG_SUCCESS_CODE);
    }

    /**
     * 推送消息
     * @param userId 接收者userId
     * @param templateId 模板id
     * @param url 详情url
     * @param data 消息内容
     * @param fromUser 发送人id
     */
    @Override
    public void pushMsg(Long userId, String templateId, String url, JSONObject data, Long fromUser){
//        WeChatTemplateMsgDTO dto = new WeChatTemplateMsgDTO()
//                .setToUser(userWxDao.queryOpenidByUserIdLately(userId))
//                .setTemplateId(templateId)
//                .setUrl(url)
//                .setData(data);
//        sendTemplateMsg(dto, fromUser);
    }

    /**
     * 发送消息
     * @param templateMsgDTO
     * @return
     */
    private WeChatTemplateMsgResponse doSend(WeChatTemplateMsgDTO templateMsgDTO) {
//        String sendTemplateMsgUrl = msgPushConfig.getWechat().getSendTemplateMsgUrl();
//        String accessToken = accessTokenManager.getCrmAccessToken();
//        sendTemplateMsgUrl = String.format(sendTemplateMsgUrl, accessToken);
//        log.debug("===========================================================\n推送模板消息: {}\nurl: {}", templateMsgDTO, sendTemplateMsgUrl);
//        try {
//            String post = HttpRequestUtils.post(sendTemplateMsgUrl, templateMsgDTO);
//            return JSON.parseObject(post, WeChatTemplateMsgResponse.class);
//        } catch (IOException e) {
//            log.error("请求微信服务器发送消息失败. url: {},\ntemplateMsgDTO: {}", sendTemplateMsgUrl, templateMsgDTO, e);
//        }
        return null;
    }
}
